package com.wind.sample.rocketmq.config;

import jakarta.annotation.PostConstruct;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RocketMQConfig {


    @Bean
    public RocketMQTemplate rocketMQTemplate(
            @Value("${rocketmq.name-server}") String nameServer,
            @Value("${rocketmq.producer.group}") String producerGroup,
            @Value("${demo.rocketmq.topic:}") String topicKey
            ) throws MQClientException {

        DefaultMQProducer producer = new DefaultMQProducer(producerGroup);
        producer.setNamesrvAddr(nameServer); // 关键注入点
        producer.setVipChannelEnabled(false); // 解决云环境问题

        // 新增关键配置
        producer.setCreateTopicKey(topicKey);  // 指定自动创建Topic的Key
        producer.setDefaultTopicQueueNums(4);      // 设置默认队列数
//        producer.start();  // 必须手动启动

        RocketMQTemplate template = new RocketMQTemplate();
        template.setProducer(producer);
        return template;
    }


//    @Value("${rocketmq.name-server:}")
//    private String nameServer;
//
//    @Value("${rocketmq.producer.group:}")
//    private String producerGroup;
//
//    @Bean(destroyMethod = "destroy")
//    public RocketMQTemplate rocketMQTemplate() {
//        RocketMQTemplate template = new RocketMQTemplate();
//
//        // 延迟初始化配置
//        template.setProducer(new DefaultMQProducer(producerGroup) {
//            @PostConstruct
//            public void delayedInit() {
//                this.setNamesrvAddr(nameServer);
//                try {
//                    this.start();
//                } catch (MQClientException e) {
//                    throw new RuntimeException("RocketMQ生产者启动失败", e);
//                }
//            }
//        });
//
//        return template;
//    }


//
//    @Value("${rocketmq.name-server}")
//    private String nameServer;
//
//    @Value("${rocketmq.producer.group}")
//    private String producerGroup;
//
//    @Bean
//    public RocketMQTemplate rocketMQTemplate() throws Exception {
//        // 创建生产者实例
//        DefaultMQProducer producer = new DefaultMQProducer(producerGroup);
//        producer.setNamesrvAddr(nameServer);
//        producer.start();
//
//        // 创建Template并设置生产者
//        RocketMQTemplate template = new RocketMQTemplate();
//        template.setProducer(producer);
//        return template;
//    }







//    @Value("${rocketmq.name-server}")
//    private String nameServer;

//    @Bean
//    public RocketMQTemplate rocketMQTemplate() {
//        RocketMQTemplate template = new RocketMQTemplate();
//        DefaultMQProducer producer = new DefaultMQProducer("DEMO_PRODUCER_GROUP");
//        producer.setNamesrvAddr("10.183.12.174:9876");
//        producer.setSendMsgTimeout(3000);
//        template.setProducer(producer);
//        return template;
//    }



//    @Value("${rocketmq.name-server}")
//    private String nameServer;
//
//    @Value("${rocketmq.producer.group}")
//    private String group;
//
//    @Bean
//    public RocketMQTemplate rocketMQTemplate() throws Exception {
//        DefaultMQProducer producer = new DefaultMQProducer(group);
//        producer.setNamesrvAddr(nameServer);
//        producer.start();
//        RocketMQTemplate template = new RocketMQTemplate();
//        template.setProducer(producer);
//        return template;
//    }




}

